package com.ks.core.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
/**
 * Created by WangLiCheng:nomad21st@163.com:KingDee Co. on 2020/12/20.
 */
public abstract class AbstractController {
    @Autowired
    HttpServletRequest request;
    /*
    也可以通过以处代码来获得HttpServletRequest
    ServletRequestAttributes ra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    HttpServletRequest request = ra.getRequest();
    * */

    /**
     * 获取Http请求中HttpHeader中的AuthorToken字段的值
     * @return
     */

    protected String GetAuthorToken(){
        return this.GetAuthorToken("AuthorToken");
    }
    protected String GetAuthorToken(String tokenName){
        return request.getHeader(tokenName);
    }
    /**
     * 获取Http请求中AuthorToken携带的信息对像
     * @return
     */
    protected OperationContext getContext() {
        return  this.getContext("AuthorToken");
    }
    protected OperationContext getContext(String tokenName){
        String token = this.GetAuthorToken(tokenName);
        if(Validator.isNullOrEmpty(token)){
            return null;
        }
        return OperationContext.create(token);
    }

}
